package org.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * @Author：无敌暴龙战士
 * @Package：org.example.config
 * @Project：learn_spring_security
 * @name：MySecurityConfig
 * @Date：2025/2/12 1:45
 * @Filename：MySecurityConfig
 * @Description: 白定义类实现用户详情服务接口
 */
@Configuration
public class MySecurityConfig {
    @Bean
    public UserDetailsService userDetailService() {

        //定义两个用户
        UserDetails apple = User.builder()
                .username("apple")
                .password(passwordEncoder().encode("123456"))
                .roles("student")
                .authorities("student:add","student:save")
                .build();
        UserDetails banana = User.builder()
                .username("banana")
                .password(passwordEncoder().encode("123456"))
                .roles("teacher")
                .authorities("teacher:add","teacher:save")
                .build();

        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
        userDetailsManager.createUser(apple);
        userDetailsManager.createUser(banana);
        return userDetailsManager;
    }


    @Bean
    public PasswordEncoder passwordEncoder(){
        //使用加密算法对密码进行加密
        return new BCryptPasswordEncoder();
    }

}


